package com.moon.game.command.guest.lair;

import java.util.ArrayList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 修建巢穴
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-5 下午6:27:05
 */
public class CreateLairCmd extends BaseCmd{

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		if (player.getLevel() < Constants.MIN_CREATE_LAIR_LEVEL) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_LEVEL, "只有" + Constants.MIN_CREATE_LAIR_LEVEL + "级以上的玩家才能修筑孵化塔！"));
			return;
		}
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			towers = new ArrayList<Tower>();
		}
		if (towers.size() >= Constants.MAX_TOWER_NUM) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_MAX_TOWER, "你的孵化塔数量已经达到上限，不能再创建！"));
			return;
		}
		
		//指定要修建的孵化塔的级别0凡兽,1仙兽,2神兽
		Integer hierarchy = Integer.parseInt(ps[0]);
		if (hierarchy == null || hierarchy > 2 || hierarchy < 0) {
			doResponsePsError(req);
			return;
		}
		
		boolean minusStatu = false;
		StringBuffer info = new StringBuffer();
		switch (hierarchy.intValue()) {
			case 0:
				minusStatu = PlayerService.minusMoney(player.getSid(), player.getId(), Constants.COMMON_ANIMAL_LAIR_PRICE);
				if (minusStatu) {
					info.append("凡兽孵化塔修建成功。碎银扣除" + Constants.COMMON_ANIMAL_LAIR_PRICE + "！");
				}else {
//					info.append("你的碎银不足" + Constants.COMMON_ANIMAL_LAIR_PRICE + "，我不能给你修筑凡兽孵化塔！");
					doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_MONEY, "你的碎银不足" + Constants.COMMON_ANIMAL_LAIR_PRICE + "，我不能给你修筑凡兽孵化塔！"));
					return;
				}
				break;
			case 1:
				minusStatu = PlayerService.minusGold(player.getSid(), player.getId(), Constants.IMMORTAL_ANIMAL_LAIR_PRICE);
				if (minusStatu) {
					info.append("仙兽孵化塔修建成功。金币扣除" + Constants.IMMORTAL_ANIMAL_LAIR_PRICE + "！");
				}else {
//					info.append("你的金币不足" + Constants.IMMORTAL_ANIMAL_LAIR_PRICE + "，我不能给你修筑仙兽孵化塔！");
					doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + Constants.IMMORTAL_ANIMAL_LAIR_PRICE + "，我不能给你修筑仙兽孵化塔！"));
					return;
				}
				break;
			case 2:
				minusStatu = PlayerService.minusGold(player.getSid(), player.getId(), Constants.GOD_ANIMAL_LAIR_PRICE);
				if (minusStatu) {
					info.append("神兽孵化塔修建成功。金币扣除" + Constants.GOD_ANIMAL_LAIR_PRICE + "！");
				}else {
//					info.append("你的金币不足" + Constants.GOD_ANIMAL_LAIR_PRICE + "，我不能给你修筑神兽孵化塔！");
					doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + Constants.GOD_ANIMAL_LAIR_PRICE + "，我不能给你修筑神兽孵化塔！"));
					return;
				}
				break;
			default:
				break;
		}
		
		if (minusStatu) {
			Tower tower = LairService.lairFactory(hierarchy);
			towers.add(tower);
			player.setTowerList(towers);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
		}
		
		req.setVO(TAG_INFO, info);
		
		//返回
		response(req, req.getClientResponse());
	}
}
